<?php

use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use models\Market;
use models\Commodity;
use utils\out;

/**获取菜场详情*/
$app->get('/market/{id}', function (Application $app, $id) {
//  return $app['twig']->render('index.html', array("marketId" => $id));
	$result = array('status'=>'ok', "result"=>"ok");
});

/** 菜场列表 **/
$app->get('/market', function (Application $app) {
	$markets =  Market::get();
	$result = array(
			'status' => 'ok',
			'result' => $markets
		);
	// cache
	return $app->json($result, 200, array('Cache-Control' => 's-maxage=' . C_CACHE_DURATION . ', public'));
	// return new Response('Foo', 200, array('Cache-Control' => 's-maxage=5'));
});


/**获取菜场所在地图上位置*/
$app->get('/market/map/{marketId}', function (Application $app, $marketId) {
	$markets =  Market::get();
	$market = null;
	$filterMarkets = array();
//	http://ptmarket.dashenw.cn/market/market/map/1020
	for ($i=0; $i<count($markets); $i++) {
		$value = $markets[$i];
		$addr = $value->address;
		// 解析地址
		$arr = preg_split('/\\r\\n/', $addr);

		if(count($arr) > 1) {
			$value->name = $arr[0];
			$value->address = isset($arr[1])?$arr[1]: '';
			$value->square = isset($arr[2])?$arr[2]: '';
			$value->junit = isset($arr[3])?$arr[3]: '';
			$value->zunit = isset($arr[4])?$arr[4]: '';
			$value->cunit = isset($arr[5])?$arr[5]: '';
			$value->gtime = isset($arr[6])?$arr[6]: '';
			$value->number = isset($arr[7])?$arr[7]: '';
			$value->contact = isset($arr[8])?$arr[8]: '';
			$value->ytime = isset($arr[9])?$arr[9]: '';
		} else {
		    $value->name = '';
            //$value->address = '';
            $value->square = '';
            $value->junit = '';
            $value->zunit = '';
            $value->cunit = '';
            $value->gtime = '';
            $value->number = '';
            $value->contact = '';
            $value->ytime = '';
		}

		if($value->marketId === $marketId) {
			$market = $value;
		} else {
			//array_push($filterMarkets, $value);
		}
	}

//	$result = array('status' => 'ok',"market" => $market, "markets" => json_encode($filterMarkets), "marketStr"=>json_encode($market));
//	return $app['twig']->render('map.html', $result, array('Cache-Control' => 's-maxage=' . C_CACHE_DURATION . ', public'));
	$result = array('status' => 'ok',"market" => $market);
	return $app->json($result);
});


/**某菜场下所有蔬菜**/
$app->get('/market/{marketId}/items', function (Application $app, $marketId) {

	$allCommodities =  Commodity::getByMarketId($marketId);

	// 将菜市场按照商品分类进行分组
	$categories = array();
	for ($j=0; $j<count($allCommodities); $j++) {
		$value = $allCommodities[$j];

		$typeId = $value->typeId;
		$typeName = $value->typeName;

		// 通过分类ID获取响应的分类
		$category = __getCommodityCategory($typeId, $categories);
		
		// 定义商品
		$commodity = (object)array(
				  "commodityId" => $value->commodityId,
		          "commodity" => $value->commodity,
		          "price" => $value->price,
		          "average" => $value->average,
		          "cdate" => $value->cdate,
		          "typeId" => $value->typeId
				);

		if($category) {
			// 添加商品
			array_push($category->commodities, $commodity);
		} else {
			// 添加商品
			$commodities = array();
			array_push($commodities, $commodity);

			// 添加商品分类
			$object = (object)array(
				'typeId' => $value->typeId, 
				'typeName' => $value->typeName, 
				'commodities' => $commodities
			);
			array_push($categories, $object);
		}
	}

	$result = array(
			'success' => true,
			'result' => array(
				'categories' => $categories
			)
		);
	return $app->json($result, 200, array('Cache-Control' => 's-maxage=' . C_CACHE_DURATION . ', public'));
});


/**********************************************************************************************/

/**通过分类ID获取分类**/
function __getCommodityCategory($typeId, $categories) {
	$result = null;
	for ($k=0; $k<count($categories); $k++) {
		$value = $categories[$k];
		if($value->typeId === $typeId) {
			$result = $value;
		}
	}
	return $result;
}